/*
* Copyright (c) 2011 Connection Road.  All rights reserved.
* This program and the accompanying material are made available
* under the terms of the Eclipse Public Licnese v1.0 which is
* available at http://www.eclipse.org/legal/epl-v10.html
*/

using System;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using CrSyncFilesForms.Utils;

namespace CrSyncFilesForms
{
    public partial class CrSyncUiVerboseOutput : Form
    {
        public CrSyncUiVerboseOutput()
        {
            InitializeComponent();

            MainBackgroundWorker.RunWorkerAsync();
            KeyPreview = true;
            KeyDown += CallBackKeyDown;
            Closing += CallBackClosing;
        }

        public void Clear()
        {
            TextBoxOutput.Text = "";
        }

        protected virtual void CallBackKeyDown(object sender, KeyEventArgs args)
        {

            // Secret save command
            if((args.Control) && (args.KeyCode == Keys.S))
            {
                var dlg = new SaveFileDialog
                              {
                                  OverwritePrompt = true,
                                  AddExtension = true,
                                  DefaultExt = ".txt",
                                  Filter = "Text file (*.txt)|*.txt",
                                  InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
                              };

                if(dlg.ShowDialog(this) == DialogResult.OK)
                {
                    using(var fileStream = dlg.OpenFile())
                    {
                        var utf8Encoder = new UTF8Encoding();
                        var verboseBytes = utf8Encoder.GetBytes(ApplicationGlobals.VerboseMessages);
                        foreach (var verboseByte in verboseBytes)
                        {
                            fileStream.WriteByte(verboseByte);
                        }
                    }
                }
            }
            else if((args.Control) && ( args.Shift) && (args.KeyCode == Keys.C))
            {
                ApplicationGlobals.VerboseMessages = "";
            }
        }

        private void PerformWork(object sender, DoWorkEventArgs e)
        {
            while(true)
            {
                System.Threading.Thread.Sleep(200);
                MainBackgroundWorker.ReportProgress(0);
            }
        }

        private void ProgressReported(object sender, ProgressChangedEventArgs e)
        {
            var scrollDown = TextBoxOutput.Text != ApplicationGlobals.VerboseMessages;
            TextBoxOutput.Text = ApplicationGlobals.VerboseMessages;
            if(scrollDown)
            {
                TextBoxOutput.SelectionStart = TextBoxOutput.Text.Length;
                TextBoxOutput.ScrollToCaret();
            }

        }

        private void CallBackClosing(object  sender, CancelEventArgs args)
        {
            try
            {
                if(Program.MainWindow != null)
                {
                    ApplicationGlobals.Verbose = false;
                    Program.MainWindow.SetVerboseWindowAsOpenState(false);
                }
            }
            catch (Exception)
            {
                //Supress
            }
        }

        
    }
}
